/**
 * Created by LeungZ on 2016/9/6.
 */
import delTmp from './form/delete.html';
import delCtrl from './form/delete.ctrl.js';

import addTmp from './form/add.html';
import addCtrl from './form/add.ctrl.js';

import permTmp from './form/perm.html';
import permCtrl from './form/perm.ctrl.js';

class Cluster {
    constructor(list, LesseeSrv, serviceSrv, clusterSrv, $uiModal, ctgNotification, $state) {
        "ngInject";
        this._uiModal = $uiModal;
        this._clusterSrv = clusterSrv;
        this._LesseeSrv = LesseeSrv;
        this._serviceSrv = serviceSrv;
        this._notification = ctgNotification;
        this._state = $state;

        if (list) {
            this.table = list;
            angular.forEach(this.table, elem => {
                this.getResource(elem);
            })
        }
    }

    getResource(elem) {
        elem.oprLoading = true;
        elem.resData = {};
        this._clusterSrv.getClusterInfo(elem.clusterCode).then(data => {
            // elem.resData = data;
            elem.resData.nodeNum = data.nodeNum;
            // elem.resData.podsNum=data.podsNum;
            // elem.resData.rcNum=data.podsNum;

            return this._LesseeSrv.getRCList({ page: 1, code: elem.clusterCode, perPage: 99 }).catch(() => {
                this._notification.error('获取租户数目有误.');
            })
        }).catch(data => {
            this._notification.error('获取容器集群详细数据有误.');
        }).then((re) => {
            elem.resData.lesNum = re.total;
            return this._serviceSrv.getDPList({ page: 1, code: elem.clusterCode, perPage: 99 }).catch(function(data) {
                ctgNotification.error('获取dp服务列表数据有误.');
            });
        }).then((re) => {
            elem.resData.rcNum = re.listMeta.totalItems;
        }).finally(() => {
            elem.oprLoading = false;
        });

    }

    getList() {
        this.tleLoading = true;
        this._clusterSrv.getClusterList().then(data => {
            this.table = data;
            angular.forEach(this.table, elem => {
                this.getResource(elem);
            })
        }).catch(data => {
            this._notification.error('获取容器集群列表数据有误.');
        }).finally(() => {
            this.tleLoading = false;
        });
    }

    deleteCluster(elem) {
        this._uiModal.open({
            templateUrl: delTmp,
            controller: delCtrl,
            controllerAs: 'vm',
            windowClass: 'alert-dialog alert-dialog-ctg',
            resolve: {
                clusterName: function() {
                    return elem.clusterName;
                }
            }
        }).result.then(() => {
            elem.oprLoading = true;
            this._clusterSrv.deleteCluster({ id: elem.id }).then(data => {
                this.getList();
                this._notification.success('删除容器集群 ' + elem.clusterName + ' 成功.')
            }).catch(data => {
                this._notification.error('删除容器集群 ' + elem.clusterName + ' 失败.');
            }).finally(() => {
                elem.oprLoading = false
            })
        });
    }

    addCluster(elem) {
        this._uiModal.open({
            templateUrl: addTmp,
            controller: addCtrl,
            controllerAs: 'vm',
            backdrop: 'static',
            resolve: {
                edit: () => elem
            }
        }).result.then(() => {
            this.getList();
        })
    }
    permCluster(elem) {
        this._uiModal.open({
            templateUrl: permTmp,
            controller: permCtrl,
            controllerAs: 'vm',
            backdrop: 'static',
            resolve: {
                tabId: () => elem.id
            }
        }).result.then((ids) => {
            this._clusterSrv.permCluster({ clusterConfigId: elem.id, sysUserIds: ids }).then((re) => {
                this._notification.success('权限分配成功');
            }).catch((re) => {
                this._notification.error('权限分配失败：' + re.resultMsg);
            })
        })
    }
}

export default Cluster;